
#include <cegfx/cenodecollection.h>


ceNodeCollection::ceNodeCollection ()
	: ceNode (NT_Collection)
{
	IQF_CONSTRUCT;
}

ceNodeCollection::~ceNodeCollection ()
{
}

IQF_IMPLEMENTATION_BEGIN(ceNodeCollection);
//IQF_IMPLEMENTATION_INTERFACE(iDynamicContent);
IQF_IMPLEMENTATION_END();


void ceNodeCollection::Update (const ceUpdateEnv& env)
{
	// nothing to do here.
}


void ceNodeCollection::BeginTransformation ()
{
	// begin the transormation;
	float det;
	D3DXMatrixDeterminant (&GetTransform().GetMatrix ());
	D3DXMatrixInverse (&inverse, &det, &GetTransform().GetMatrix ());
}

void ceNodeCollection::EndTransformation ()
{
	D3DXMATRIX tmp;
	ceBoundingBox& bbox = GetBoundingBox();
	bbox.Clear();

	ceNodeVector::iterator nit;
	for (nit = nodes.begin (); nit != nodes.end (); nit++)
	{
		ceNode* node = *nit;

		// start the transformation of this node
		node->BeginTransformation ();
		// transform the global matrix of the node in the local space of the collection
		D3DXMatrixMultiply (&tmp, &node->GetTransform ().GetMatrix (), &inverse);

		// and back in the global space with the new transformation.
		D3DXMatrixMultiply (&node->GetTransform ().GetMatrix (), &tmp, &GetTransform().GetMatrix ());

		// and finish the node
		node->EndTransformation ();
		bbox.AddBoundingBox (node->GetBoundingBox ());
	}
	bbox.Update ();
}

void ceNodeCollection::AddNode (ceNodePtr node)
{
	if (node->GetNodeType () != ceNode::NT_LightNode)
	{
		nodes.push_back (node);
	}
}

void ceNodeCollection::RemoveNode (ceNodePtr node)
{
	ceNodeVector::iterator it;
	for (it = nodes.begin (); it != nodes.end (); it++)
	{
		if (*it == node)
		{
			nodes.erase (it);
			return;
		}
	}
}


unsigned ceNodeCollection::GetNumberOfNodes () const
{
	return nodes.size ();
}

ceNodePtr ceNodeCollection::GetNode (unsigned idx)
{
	return nodes [idx];
}

const ceNodePtr ceNodeCollection::GetNode (unsigned idx) const
{
	return nodes [idx];
}
/*
void ceNodeCollection::AddNodeCollection (ceNodeCollectionPtr collection)
{
	collections.push_back (collection);
}

void ceNodeCollection::RemoveNodeColletion (ceNodeCollectionPtr collection)
{
	ceNodeCollectionVector::iterator it;
	for (it = collections.begin (); it != collections.end (); it++)
	{
		if (*it == collection)
		{
			collections.erase (it);
			return;
		}
	}
}

unsigned ceNodeCollection::GetNumberOfNodeCollections () const
{
	return collections.size ();
}

ceNodeCollectionPtr ceNodeCollection::GetNodeCollection (unsigned idx)
{
	return collections[idx];
}

const ceNodeCollectionPtr ceNodeCollection::GetNodeCollection (unsigned idx) const
{
	return collections[idx];
}


void ceNodeCollection::DynamicLoad ()
{
}

void ceNodeCollection::DynamicUnload ()
{
}
*/

